//
//  DDMyGiftDetail.m
//  dingdang
//
//  Created by Jiangxp on 15/8/25.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDMyGiftDetail.h"
#import "UIImageView+AFNetworking.h"
#import "UIViewController+MJPopupViewController.h"
#import "DDMyGiftDetailTableViewCell.h"
#import "DDNetWorkAPIClient.h"

@interface DDMyGiftDetail ()
{
    UIImageView *imageV;
    UILabel *labName;
    UILabel *labPrice;
}
@end

@implementation DDMyGiftDetail
@synthesize info;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self createData];
    
}

- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:@"我的礼品" parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    CGFloat mainWidth = [UIScreen mainScreen].bounds.size.width;
    
    CGRect rect = CGRectMake(0, 0, mainWidth, 330);
    UIView *headerView = [[UIView alloc] initWithFrame:rect];
    _listView.tableHeaderView = headerView;
    
    rect = CGRectMake((mainWidth - 177)/2, 20, 200, 280);
    imageV = [[UIImageView alloc] initWithFrame:rect];
    imageV.contentMode = UIViewContentModeScaleAspectFit;
    [headerView addSubview:imageV];
    
    rect = CGRectMake(0, 285, mainWidth, 10);
    labName = [[UILabel alloc] initWithFrame:rect];
    labName.backgroundColor = [UIColor clearColor];
    labName.font = [UIFont systemFontOfSize:16.0f];
    labName.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:labName];
    
    rect = CGRectMake(0, 310, mainWidth/2, 10);
    UILabel *labJiFen = [[UILabel alloc] initWithFrame:rect];
    labJiFen.backgroundColor = [UIColor clearColor];
    labJiFen.font = [UIFont systemFontOfSize:18.0f];
    labJiFen.textAlignment = NSTextAlignmentRight;
    labJiFen.text = @"积分：";
    [headerView addSubview:labJiFen];

    rect = CGRectMake(mainWidth/2, 310, mainWidth/2, 15);
    labPrice = [[UILabel alloc] initWithFrame:rect];
    labPrice.backgroundColor = [UIColor clearColor];
    labPrice.font = [UIFont systemFontOfSize:18.0f];
    labPrice.textAlignment = NSTextAlignmentLeft;
    labPrice.textColor = [UIColor orangeColor];
    [headerView addSubview:labPrice];
    
    _listView.dataSource = self;
    _listView.delegate = self;
    _listView.allowsSelection = NO;
}

- (void)createData
{
    [imageV setImageWithURL:[NSURL URLWithString:info.imgPath]];
    labName.text = info.name;
    labPrice.text = [NSString stringWithFormat:@"%@", info.price];
}

#pragma mark -UITableDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 || indexPath.row == 2) {
        return 90;
    }
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"UITableViewCell";
    DDMyGiftDetailTableViewCell *cell = (DDMyGiftDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        [tableView registerNib:[UINib nibWithNibName:@"DDMyGiftDetailTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    }
    
    switch (indexPath.row) {
        case 0:
        {
            cell.labTitle.text = @"礼品简介：";
            [cell setIntroductionText:info.detail];
            break;
        }
        case 1:
            cell.labTitle.text = @"状态：";
            [cell setIntroductionText:[DDMGiftOrder getStartStr:info.status]];
            break;
        case 2:
        {
            cell.labTitle.text = @"收货地址：";
            NSString *str = [NSString stringWithFormat:@"%@\n%@", info.consignee, info.address];
            [cell setIntroductionText:str];
            break;
        }
        case 3:
        {
            cell.labTitle.text = @"下单时间：";
//            cell.labContent.text = info.orderTime;
            NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
            [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss "];
            double unixTimeStamp = [info.orderTime doubleValue]/1000;
            NSTimeInterval _interval=unixTimeStamp;
            NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
            [dateformatter setLocale:[NSLocale currentLocale]];
            cell.labContent.text = [dateformatter stringFromDate:date];
            break;
        }
        case 4:
            cell.labTitle.text = @"完成时间：";
            [cell setIntroductionText:info.overTime];
            break;
        default:
            break;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end
